/**
 * 我的
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/5
 */
var self = memberInfo = version = '';

document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview();
	
	memberInfo = plus.storage.getItem('memberInfo');
	
	memberInfo = $.parseJSON(memberInfo); 
	
	if(memberInfo.nickname)$('.nickname').text(memberInfo.nickname);
	
	if(memberInfo.avatar)$('.pic').attr('src' , getImgUrl(memberInfo.avatar));
	
	//前往修改个人资料 
	$('.memberInfo').on('click' , function (){ 
		plus.nativeUI.showWaiting('加载中...');
		var member_window = plus.webview.create('me-inf-revise.html' , 'me-inf-revise' , {} , {self_window : self.id}); 
		member_window.addEventListener('loaded' , function(){
			member_window.show('slide-in-bottom');
			plus.nativeUI.closeWaiting();
		})
		
	})
	
	//获取当前版本
	plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo){
		
		version = wgtinfo.version;
		
		$('.version').text('当前版本' +  wgtinfo.version); 
		
	});
	
	//检测更新
	$('.update').on('click' , function(){
		mui.getJSON(webRoot + '/api/Public/update/update.json', 
			function(data) {
				var os = plus.os.name;
				if(data[os].version > version){
					plus.nativeUI.confirm(data[os].note, function(event) {
						if (0 == event.index) {
							plus.runtime.openURL(data[os].url);
						}
					}, data.title, ["立即更新", "取　　消"]);
				}else{
					toast('当前已是最新版本,无需更新');
				}
		});
	})
	
	
	//注销帐号
 	$('.login_out').on('click' , function(){
 		plus.nativeUI.confirm('温馨提示: 	确定要注销该帐号吗？' , function(e){
 			if(e.index == 0){
 				plus.storage.removeItem('memberInfo');
 				toast('注销成功');
 				if(plus.os.name == 'Android'){plus.runtime.quit();}
 				else{openNewpage('login.html');}
 			}
 		} , '第三方咨询' , ['确定' , '取消'])
 	})
 	
 	
 	
 	//点击分享
	$('.share').each(function(index) { 
		var type = ex = content = ''  
		$(this).on('click', function() { 
			if(index == 0) {type = 'weixin' ; ex = 'WXSceneSession'; content = '';} 
			if(index == 1) {type = 'qq' ; ex = ''; content = $('.content').text() ; } 
			if(index == 2) {type = 'weixin' ; ex = 'WXSceneTimeline';content = '';}
			shareAction(type, ex , '' , '端砚生物' , '' , '' , memberInfo.aid , '');
		})
	}) 
 	
 	//设置
 	$('.set').each(function(index){
 		$(this).on('click' , function(){
 			var tid = index == 0 ? 60 : (index == 1 ? 61 : 62);
 			plus.nativeUI.showWaiting();
 			var html = plus.webview.create('about.html' , 'about' , {} , {tid : tid , title : $(this).find('.title').text()});
 			html.addEventListener('loaded' , function(){
 				html.show('pop-in' , 300);
 				plus.nativeUI.closeWaiting();
 			})
 		})
 	}) 
 	
 	loadNewData();
 	
}


function loadNewData(){ 
	var flag = 	plus.storage.getItem('newMsg');
	if(flag == 'false'){
		$('.red-circle').css('display' , 'none');
	}else{
		$('.red-circle').css('display' , 'inline-block');
	}
	setTimeout('loadNewData()' , 10000);
//	setInterval(loadNewData() , 60000);
}


window.addEventListener('updateInfo' , function(data){
	
	$('.nickname').text(data.detail.nickname);
	
	$('.pic').attr('src' , getImgUrl(data.detail.pic)); 
	
})
